package gitapp.oschina.net.my_osc;

import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.kymjs.okhttp.OkHttpStack;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.http.RequestQueue;
import com.squareup.okhttp.OkHttpClient;

import java.io.File;
import java.util.UUID;

import gitapp.oschina.net.my_osc.bean.Follow;
import gitapp.oschina.net.my_osc.bean.User;
import gitapp.oschina.net.my_osc.common.StringUtils;

import static gitapp.oschina.net.my_osc.common.Contanst.*;

/**
 * @author Tristan-Hou
 */
public class AppContext extends Application{

    private boolean login = false;  //登录状态
    private static AppContext appContext;
    private int loginUid = 0; // 登录用户的id

    public static AppContext getInstance(){
        return appContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        File cacheFolder = getCacheDir();
        RxVolley.setRequestQueue(RequestQueue.newRequestQueue(cacheFolder, new OkHttpStack(new OkHttpClient())));
        init();
        appContext = this;
    }

    private void init(){
        User loginUser = getLoginInfo();
        if (null != loginUser && StringUtils.toInt(loginUser.getId())>0
                && !StringUtils.isEmtry(getProperty(PROP_KEY_PRIVATE_TOKEN))){
            this.loginUid = StringUtils.toInt(loginUser.getId());
            this.login = true;
        }
    }

    public void setProperty(String key, String value){
        AppConfig.getAppConfig(this).set(key, value);
    }

    public String getProperty(String key){
        return AppConfig.getAppConfig(this).get(key);
    }

    public String getAppId(){
        String uniqueID = getProperty(AppConfig.CONF_APP_UNIQUEID);
        if (StringUtils.isEmtry(uniqueID)){
            uniqueID = UUID.randomUUID().toString();
            setProperty(AppConfig.CONF_APP_UNIQUEID, uniqueID);
        }
        return uniqueID;
    }

    public PackageInfo getPackageInfo(){
        PackageInfo info = null;
        try {
            info = getPackageManager().getPackageInfo(getPackageName(), 0);
        }catch (PackageManager.NameNotFoundException e){
            e.printStackTrace(System.err);
        }
        if (info == null){
            info = new PackageInfo();
        }
        return info;
    }

    /**
     * 获取登录信息
     * @return
     */
    public User getLoginInfo(){
        User user = new User();
        user.setId(getProperty(PROP_KEY_UID));
        user.setUsername(getProperty(PROP_KEY_USERNAME));
        user.setName(getProperty(PROP_KEY_NAME));
        user.setBio(getProperty(PROP_KEY_BIO));
        user.setWeibo(getProperty(PROP_KEY_WEIBO));
        user.setBlog(getProperty(PROP_KEY_BLOG));
        user.setTheme_id(StringUtils.toInt(getProperty(PROP_KEY_THEME_ID), 1));
        user.setState(getProperty(PROP_KEY_STATE));
        user.setCreated_at(getProperty(PROP_KEY_CREATED_AT));
        user.setPortrait(getProperty(PROP_KEY_PORTRAIT));
        user.setNew_portrait(getProperty(PROP_KEY_NEWPORTRAIT));
        user.setIsAdmin(StringUtils.toBool(getProperty(PROP_KEY_IS_ADMIN)));
        user.setCanCreateGroup(StringUtils
                .toBool(getProperty(PROP_KEY_CAN_CREATE_GROUP)));
        user.setCanCreateProject(StringUtils
                .toBool(getProperty(PROP_KEY_CAN_CREATE_PROJECT)));
        user.setCanCreateTeam(StringUtils
                .toBool(getProperty(PROP_KEY_CAN_CREATE_TEAM)));
        Follow follow = new Follow();
        follow.setFollowers(StringUtils.toInt(getProperty(ROP_KEY_FOLLOWERS)));
        follow.setStarred(StringUtils.toInt(getProperty(ROP_KEY_STARRED)));
        follow.setFollowing(StringUtils.toInt(getProperty(ROP_KEY_FOLLOWING)));
        follow.setWatched(StringUtils.toInt(getProperty(ROP_KEY_WATCHED)));
        user.setFollow(follow);
        return user;
    }

    public boolean isLogin(){
        return login;
    }

    public boolean isCheckUp(){
        String perf_checkup = getProperty(AppConfig.CONF_CHECKUP);
        return StringUtils.isEmtry(perf_checkup) || StringUtils.toBool(perf_checkup);
    }

    public void setConfigCheckUp(boolean b){
        setProperty(AppConfig.CONF_CHECKUP, String.valueOf(b));
    }

    public boolean isReceiveNotice(){
        String perf_notice = getProperty(AppConfig.CONF_RECEIVENOTICE);
        return StringUtils.isEmtry(perf_notice) || StringUtils.toBool(perf_notice);
    }

}
